﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.ASPxUploadControl;
using System.IO;
using DevExpress.Web.Mvc;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 系统属性
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class WebsitePropController : AdminBaseController
    {
        /// <summary>
        /// 系统属性
        /// </summary>
        /// <returns>系统属性主页面</returns>
        [AuthorityActionFilter("m_WebsiteProp", Order = 1)]
        [LogPageActionFilter("系统属性", Order = 2)]
        public ActionResult Index()
        {
            BLL.Entity entityBLL = new BLL.Entity();
            Model.Entity entityModel = entityBLL.GetModel(GlobalConstant.ENTITYNO);
            string strWhere = "FEntityNo='" + GlobalConstant.ENTITYNO + "'";
            if (entityModel != null)
            {
                BLL.SysParam sysParamBLL = new BLL.SysParam();
                List<Model.SysParam> sysParamList = sysParamBLL.GetModelList(strWhere);

                foreach (Model.SysParam sysParam in sysParamList)
                {
                    switch (sysParam.FFParamNo)
                    {
                        case "OA_SSOUse"://是否启用门户验证
                            {
                                ViewData["OA_SSOUse"] = sysParam.FFValue;
                                break;
                            }
                        case "OA_SSOLoginAddress"://门户验证提交地址
                            {
                                ViewData["OA_SSOLoginAddress"] = sysParam.FFValue;
                                break;
                            }
                        case "OA_TASK_CHECK_DAYS"://文件阅办和重点督查超期自动审批天数
                            {
                                ViewData["OA_TASK_CHECK_DAYS"] = sysParam.FFValue;
                                int days = 0;
                                if (!int.TryParse(ViewData["OA_TASK_CHECK_DAYS"].ToString(), out days))
                                {
                                    ViewData["OA_TASK_CHECK_DAYS"] = null;
                                }
                                break;
                            }
                    }

                }


                ViewData["FIsShowGrid"] = "N";//默认值
                string tableName = "t_accManagerDoc";
                string fildeName = "FShowDate";
                BLL.UsrDataDictionary usrDataDictionaryBLL = new BLL.UsrDataDictionary();
                Model.UsrDataDictionary usrDataDictionaryModel = usrDataDictionaryBLL.GetModel(tableName, fildeName, GlobalConstant.ENTITYNO);
                if (usrDataDictionaryModel != null)
                {
                    ViewData["FIsShowGrid"] = usrDataDictionaryModel.FIsShowGrid;
                }
                else
                {
                    usrDataDictionaryModel = new Model.UsrDataDictionary();
                    usrDataDictionaryModel.FEntityNo = GlobalConstant.ENTITYNO;
                    usrDataDictionaryModel.FFieldName = fildeName;
                    usrDataDictionaryModel.FInputMethod = "M";
                    usrDataDictionaryModel.FIsRequire = "Y";
                    usrDataDictionaryModel.FIsShowForm = "N";
                    usrDataDictionaryModel.FIsShowGrid = "N";
                    usrDataDictionaryModel.FSysAttribute = "时间";
                    usrDataDictionaryModel.FTableName = tableName;
                    usrDataDictionaryModel.FUserAttribute = "时间";

                    try
                    {
                        usrDataDictionaryBLL.Add(usrDataDictionaryModel);
                    }
                    catch (Exception)
                    {

                    }
                }
                ViewData["OA_SSOUse"] = ViewData["OA_SSOUse"] == null ? "N" : ViewData["OA_SSOUse"].ToString();
                ViewData["OA_SSOLoginAddress"] = ViewData["OA_SSOLoginAddress"] == null ? "" : ViewData["OA_SSOLoginAddress"].ToString();
                ViewData["FIsShowGrid"] = ViewData["FIsShowGrid"] == null ? "N" : ViewData["FIsShowGrid"].ToString();
                ViewData["OA_TASK_CHECK_DAYS"] = ViewData["OA_TASK_CHECK_DAYS"] == null ? "5" : ViewData["OA_TASK_CHECK_DAYS"].ToString();//默认为5天
                //ViewData[""]=entityModel.FWaterMarkSvrFileName
                return View("Index", entityModel);
            }
            else
            {
                return null;
            }

        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entityModel">系统信息</param>
        /// <returns>保存结果</returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Save(Model.Entity entityModel)
        {
            //bool isSuccess = true;
            string errorMessage = "";
            ActionResult actionResult = null;
            //系统参数
            List<Model.SysParam> sysParamList = new List<Model.SysParam>();
            sysParamList.Add(new Model.SysParam()
            {
                FEntityNo = GlobalConstant.ENTITYNO,
                FFParamNo = "OA_SSOUse",
                FFValue = CheckBoxExtension.GetValue<bool>("OA_SSOUse") == true ? "Y" : "N"
            });


            if (!string.IsNullOrEmpty(TextBoxExtension.GetValue<string>("OA_SSOLoginAddress")))
            {
                sysParamList.Add(new Model.SysParam()
                {
                    FEntityNo = GlobalConstant.ENTITYNO,
                    FFParamNo = "OA_SSOLoginAddress",
                    FFValue = TextBoxExtension.GetValue<string>("OA_SSOLoginAddress").Trim()
                });
            }
            else
            {
                sysParamList.Add(new Model.SysParam()
                {
                    FEntityNo = GlobalConstant.ENTITYNO,
                    FFParamNo = "OA_SSOLoginAddress",
                    FFValue = ""
                });
            }
            sysParamList.Add(new Model.SysParam()
            {
                FEntityNo = GlobalConstant.ENTITYNO,
                FFParamNo = "OA_TASK_CHECK_DAYS",
                FFValue = TextBoxExtension.GetValue<int>("OA_TASK_CHECK_DAYS").ToString()
            });
            Model.UsrDataDictionary usrDataDictionaryModel = new Model.UsrDataDictionary();
            usrDataDictionaryModel.FEntityNo = GlobalConstant.ENTITYNO;
            usrDataDictionaryModel.FFieldName = "FShowDate";
            usrDataDictionaryModel.FTableName = "t_accManagerDoc";
            usrDataDictionaryModel.FIsShowGrid = CheckBoxExtension.GetValue<bool>("FIsShowGrid") == true ? "Y" : "N";
            entityModel.FCopyRight = entityModel.FCopyRight.Replace("\r\n", "");
            BLL.Entity entityBLL = new BLL.Entity();
            BLL.SysParam sysParamBLL = new BLL.SysParam();
            BLL.UsrDataDictionary usrDataDictionaryBLL = new BLL.UsrDataDictionary();
            Model.Entity oldEntityModel = entityBLL.GetModel(entityModel.FEntityNo);
            try
            {
                entityBLL.UpdateEntity(entityModel.FEntityNo, entityModel);
                sysParamBLL.SaveSysparam(sysParamList);
                usrDataDictionaryBLL.UpdateIsShowGrid(usrDataDictionaryModel);
                //更新缩略图相关
                string oldSrvName = "";
                if (entityModel.FWaterMarkCls == "1")
                {
                    UploadedFile[] fileList = UploadControlExtension.GetUploadedFiles("PhotoUpload");
                    if (null != fileList && fileList.Length > 0)
                    {
                        UploadedFile upload = fileList[0];
                        if (upload.ContentLength > 0)
                        {
                            string fileSavePath = System.Configuration.ConfigurationManager.AppSettings["FileSavePath"];
                            oldSrvName = oldEntityModel.FWaterMarkSvrFileName;
                            string strImageFileName = FileUpload.UploadFile(upload, fileSavePath + GlobalConstant.ENTITYNO + "/image", ".jpg.gif");

                            entityModel.FWaterMarkContent = upload.FileBytes;
                            entityModel.FWaterMarkSvrFileName = fileSavePath + GlobalConstant.ENTITYNO + "/image/" + strImageFileName;
                            entityModel.FWaterMarkExt = upload.ContentType;
                            entityModel.FWaterMarkOldFileName = Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();

                            entityBLL.UpdateEntityWaterFile(entityModel.FEntityNo, entityModel.FWaterMarkContent,
                                entityModel.FWaterMarkSvrFileName, entityModel.FWaterMarkExt, entityModel.FWaterMarkOldFileName);
                            if (oldSrvName != "")//删除原图片
                            {
                                GenerateUtil.DeleteFile(oldSrvName);
                            }
                        }
                    }
                }
                else
                {
                    oldSrvName = entityModel.FWaterMarkSvrFileName;

                    entityModel.FWaterMarkContent = null;
                    entityModel.FWaterMarkSvrFileName = "";
                    entityModel.FWaterMarkExt = "";
                    entityModel.FWaterMarkOldFileName = "";

                    entityBLL.UpdateEntityWaterFile(entityModel.FEntityNo, entityModel.FWaterMarkContent,
                        entityModel.FWaterMarkSvrFileName, entityModel.FWaterMarkExt, entityModel.FWaterMarkOldFileName);
                    if (oldSrvName != "" && oldSrvName != null)//删除原图片
                    {
                        GenerateUtil.DeleteFile(oldSrvName);
                    }
                }


                ViewData["RedirectURL"] = Url.Action("Index", "WebsiteProp");

                actionResult = View("SuccessMessagePage");

            }
            catch (Exception ee)
            {
                errorMessage = "操作异常:" + ee.Message;
            }
            if (actionResult == null)
            {
                ViewData["message"] = errorMessage;
                ViewData["OA_SSOUse"] = sysParamList[0].FFValue;
                ViewData["OA_SSOLoginAddress"] = sysParamList[1].FFValue;
                ViewData["FIsShowGrid"] = usrDataDictionaryModel.FIsShowGrid;
                actionResult = View("Index", entityModel);
            }
            return actionResult;
        }

    }
}
